<template>
    <div class="aside-box">
     <el-aside :width="width" class="aside" >
      <el-menu  
        background-color="#555c64"
        text-color="#fff"
        :collapse="isCollapase"
        :collapse-transition="false"
        :default-active="activeMenu"
      >
        <h3 v-show="!isCollapase">通用后台管理系统</h3>
        <h3 v-show="isCollapase">后台</h3>
        <!-- 一级子菜单 -->
            <el-menu-item 
                v-for="item in noChildren"
                :index="item.path"
                :key="item.path"
                @click="handleMenu(item)">
                    <component class="icons" :is="item.icon"></component>
                    <span>{{item.label}}</span>
            </el-menu-item>

            <!-- 二级子菜单 -->
            <el-sub-menu 
            v-for="item in hasChildren"
            :index="item.path"
            :key="item.path"
            >
            <template #title>
                <component class="icons" :is="item.icon"></component>
                <span>{{item.label}}</span>
            </template>
            <el-menu-item-group>
                <el-menu-item 
                v-for="(subItem,subIndex) in item.children"
                :index="subItem.path"
                :key="subItem.path"
                @click="handleMenu(subItem)">
                  <component class="icons" :is="subItem.icon"></component>
                  <span>{{subItem.label}}</span>
                </el-menu-item>
            </el-menu-item-group>
          </el-sub-menu>
        </el-menu>
      </el-aside>
    </div>

</template>

<script setup>
import { ref, reactive, toRefs, onMounted, computed} from 'vue'
import { userAllDateStore } from '../stores/index'
import {useRoute,useRouter} from 'vue-router'


const store = userAllDateStore()
// 定义路由对象
const router = useRouter()
// 定义路由规则
const route = useRoute()
// 左侧栏点击对应的标签，可以跳转到对应的页面
const handleMenu = (item)=>{
    // 将相应的页面路径放到路由中
    router.push(item.path)
    // 将标签添加
    store.selectMenu(item)
}
// 刷新后，仍显示当前标签页
const activeMenu = computed(()=>route.path)

// const list =ref([
//         {
//           path: '/home',
//           name: 'home',
//           label: '首页',
//           icon: 'house',
//           url: 'Home'
//         },
//         {
//             path: '/mall',
//             name: 'mall',
//             label: '商品管理',
//             icon: 'video-play',
//             url: 'Mall'
//         },
//         {
//             path: '/user',
//             name: 'user',
//             label: '用户管理',
//             icon: 'user',
//             url: 'User'
//         },
//         {
//             path: 'other',
//             label: '其他',
//             icon: 'location',
//             children: [
//                 {
//                     path: '/page1',
//                     name: 'page1',
//                     label: '页面1',
//                     icon: 'setting',
//                     url: 'Page1'
//                 },
//                 {
//                     path: '/page2',
//                     name: 'page2',
//                     label: '页面2',
//                     icon: 'setting',
//                     url: 'Page2'
//                 }
//             ]
//         }
//  ])

const list = computed(()=>store.state.menuList)
 const noChildren = computed(() => list.value.filter(item => !item.children))
 const hasChildren =computed(() => list.value.filter(item => item.children))
//  const clickMenu=(item)=>{
//     router.push(item.path)
//  }

// 菜单栏折叠设置
const isCollapase = computed(()=>store.state.isCollapase)

const width = computed(()=>store.state.isCollapase?'64px':'180px')

</script>

<style scoped lang="less">
.aside-box {
    height: 100vh;
    background-color: #555c64;
}

.icons {
    width: 18px;
    height: 18px;
    margin-right: 5px;
}

.el-menu {
    border-right: none;
}


h3 {
    line-height: 48px;
    color: #fff;
    text-align: center;
}

</style>